<%@ Page PathInChannel="ajax/course/view{Ext}" RunAt="Execute" %>
<ali:List ID="Member" Mark="Course" Object="Member" Page="1" PageSize="1" Condition="IsDeleted=false and (ExpiredTime IS NULL or ExpiredTime>DateTime.Now) and UserID=User['ID']" />
<ali:Get ID="MemberLevel" Mark="Course" Object="MemberLevel" Guid="Member[LevelID]" />
<ali:List ID="Promotion" Mark="Course" Object="Promotion" Condition="IsDeleted=false and CourseID=Request.QueryString['CourseID'] and StartTime<DateTime.Now and EndTime>DateTime.Now" />
<ali:List ID="CourseStudy" Mark="Course" Object="CourseStudy" Page="1" PageSize="1" Condition="CourseID=Request.QueryString['CourseID'] and UserID=User['ID']" />
<ali:List ID="CourseOrder" Mark="Course" Object="Order" Page="1" PageSize="1" Condition="IsArchived=false and IsCanceled=false and IsDelivered=2 and (ExpiredTime IS NULL or ExpiredTime>DateTime.Now) and UserID=User['ID'] and CourseID=Request.QueryString['CourseID']"/>
<ali:Get ID="AskCount" Mark="Course" Object="Ask" Operation="GetRecordCount(AliCMS.CourseModule.Alias.Ask.CourseID + '=' + Request.QueryString['CourseID'])"/>
<ali:Get ID="Distributer" Mark="User" Object="User" Guid="Request.QueryString['DistributerID']"/>
<ali:List ID="Agent" Mark="Course" Object="Member" Page="1" PageSize="1" Condition="IsDeleted=false and (ExpiredTime IS NULL or ExpiredTime>DateTime.Now) and UserID=Request.QueryString['DistributerID']" />
<ali:Get ID="AgentLevel" Mark="Course" Object="MemberLevel" Guid="Agent[LevelID]" />
{
	"askCount":<%=AskCount%>
	<%If Convert.ToDecimal(Request.QueryString["Price"]) != 0%>
		<%If CourseOrder[Count] > 0 || Convert.ToBoolean(Request.QueryString["IsMemberFree"]) && Member[Count] > 0%>
			,"hasBuy":true
			<%If CourseOrder[Count] > 0 && !Convert.IsDBNull(CourseOrder[ExpiredTime])%>
				,"days":<%=(Convert.ToDateTime(CourseOrder[ExpiredTime]) - DateTime.Now).Days%>
			<%EndIf%>
			<%If CourseStudy[Count] > 0 %>
				,"courseStudy":{"id":<%=CourseStudy[InnerID]%>,"value":<%=CourseStudy[Value]%>,"max":"<%=CourseStudy[Max]%>","startTime":"<%=CourseStudy[StartTime]%>","endTime":"<%=CourseStudy[EndTime]%>","lastLessonId":"<%=CourseStudy[LastLessonID]%>"}
			<%Else%>
				,"courseStudy":<%=AddCourseStudy()%>
			<%EndIf%>
		<%Else%>
			,"hasBuy":false
			<ali:List Mark="User" Object="User" Page="1" PageSize="1" Condition="IsDeleted=false and ID=User['ID']">
				<ali:Record>
					,"userNickname":"<%=Nickname%>"
					,"userHead":"<%=Head%>"
				</ali:Record>
			</ali:List>
			<%If !String.IsNullOrEmpty(Request.QueryString['DistributerID'])%>
				,"distributerId":"<%=Distributer[ID]%>"
				,"distributerName":"<%=Distributer[Name]%>"
				,"distributerNickname":"<%=Distributer[Nickname]%>"
				,"distributerHead":"<%=Distributer[Head]%>"
				<%If Agent[Count] == 0%>
					,"distributerIsAgent":false
				<%Else%>
					,"distributerIsAgent":true
					,"agentCommision":<%=AgentLevel[Commision]%>
				<%EndIf%>
			<%EndIf%>
			<%If isMemberPay %>
				,"isMemberPay":true
				,"memberPrice":<%=MemberPrice%>
				,"memberCommission": <%=Convert.ToDecimal(Request.QueryString["Price"]) * Convert.ToDecimal(MemberLevel[Commision]) / 100%>
			<%ElseIf Promotion[Count] > 0%>
				,"now":"<%=DateTime.Now%>"
				,"promotion":{"name":"<%=Promotion[Name]%>","startTime":"<%=Promotion[StartTime]%>","endTime":"<%=Promotion[EndTime]%>","price":<%=Promotion[Price]%>}
			<%EndIf%>
			<ali:List Mark="System" Object="Payment" Condition="Type='WEIXIN_PAY_BY_WAP'">
				<ali:Record>
					,"paymentId":"<%=ID%>"
				</ali:Record>
			</ali:List>
		<%EndIf%>
	<%EndIf%>
	<ali:List Mark="Course" Object="Collect" Page="1" PageSize="1" Condition="CourseID=Request.QueryString['CourseID'] and UserID=User['ID']">
		<ali:Record>
			,"collectId":"<%=ID%>"
		</ali:Record>
	</ali:List>
}
<script language="c#" runat="server">
protected decimal _memberPrice = -1;
protected decimal MemberPrice
{
	get
	{
		if (_memberPrice >= 0)
			return _memberPrice;
			
		if (<%=Member[Count]%> > 0 && Convert.ToDecimal(MemberLevel[AliCMS.CourseModule.Alias.MemberLevel.Commision]) > 0)
			_memberPrice = Convert.ToDecimal(Request.QueryString["Price"]) - Convert.ToDecimal(Request.QueryString["Price"]) * Convert.ToDecimal(MemberLevel[AliCMS.CourseModule.Alias.MemberLevel.Commision]) / 100;
		else
			_memberPrice = 0;
		
		return _memberPrice;
	}
}
protected bool isMemberPay
{
	get
	{
		return MemberPrice > 0 && (<%=Promotion[Count]%> == 0 || <%=Promotion[Count]%> > 0 && MemberPrice < Convert.ToDecimal(Promotion.Rows[0][AliCMS.CourseModule.Alias.Promotion.Price]));
	}
}
protected string AddCourseStudy()
{
	using (AliCMS.CourseModule.ICourseStudy bll = (AliCMS.CourseModule.ICourseStudy)Manager.CreateObject("CourseStudy", InstanceID, PageLang, false))
	{
		Dictionary<string, object> item = new Dictionary<string, object>();
		item[AliCMS.CourseModule.Alias.CourseStudy.UserID] = new Guid(Manager.User["ID"]);
		item[AliCMS.CourseModule.Alias.CourseStudy.CourseID] = new Guid(Request.QueryString["CourseID"]);
		item[AliCMS.CourseModule.Alias.CourseStudy.CourseName] = Request.QueryString["CourseName"];
		item[AliCMS.CourseModule.Alias.CourseStudy.CourseThumbnail] = Request.QueryString["CourseThumbnail"];
		Guid guid = bll.Add(item);
		
		return "{\"id\":" + bll.GetInnerIDFromGuid(guid) + ",\"value\":0,\"max\":\"\",\"startTime\":\"\",\"endTime\":\"\",\"lastLessonId\":\"\"}";
	}
}
</script>